package com.logicbus.backend.bizlog.handler;

import org.w3c.dom.Element;

import com.anysoft.rrm.RRData;
import com.anysoft.rrm.RRDataHandlerFactory;
import com.anysoft.rrm.ServiceMetrics;
import com.anysoft.stream.AbstractHandler;
import com.anysoft.stream.Handler;
import com.anysoft.util.Properties;
import com.logicbus.backend.bizlog.BizLogItem;
import com.logicbus.backend.bizlog.BizLogger;

/**
 * 针对BizLog统计服务指标
 * @author duanwaiwai
 * @since  1.6.12.34 [20190507] <br>
 */
public class ServiceBizLogger extends AbstractHandler<BizLogItem>implements BizLogger {

	protected Handler<RRData> handler = RRDataHandlerFactory.getHandler();
	
	protected String getMetricsId(String svcId){
		return "svc.thpt:" + svcId;
	}	
	
	protected void onHandle(BizLogItem data, long t) {
		if (handler != null){
			String id = data.id;
	
			// 统计服务调用次数
			String metricsId = getMetricsId(id);
			ServiceMetrics sm = new ServiceMetrics(metricsId,id);
			sm.count(data.duration, !data.result.equals("core.ok"));
			handler.handle(sm, t);
	
			ServiceMetrics total = new ServiceMetrics("metrics.service","/all");
			total.count(data.duration, !data.result.equals("core.ok"));
			handler.handle(total,t);
		}
		
	}

	protected void onFlush(long t) {
		// 没有什么可以flush的
	}

	protected void onConfigure(Element e, Properties p) {
		
	}
	
}
